package society.framework.android;

import society.framework.core.communication.MessageRouter;
import android.app.Activity;
import android.os.Bundle;

/**
 * Activity which takes care of society creation and management. Extend this activity to use the framework.
 * @author Micu Andrei
 *
 */
public abstract class SocietyActivity extends Activity {
	protected AndroidSocietyManager societyManager;
	
	public AndroidSocietyManager getSocietyManager() {
		return societyManager;
	}

	/**
	 * In onCreate() method the society tries to load the previous state of the society. If it fails, the initializeSociety() 
	 * method is called. Then the automatons are started.<br/>
	 * Don't override this method unless you know what you are doing.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		societyManager = new AndroidSocietyManager(this);
		if(!societyManager.loadState()){
			societyManager.clear();
			MessageRouter.getInstance().clearSubscribers();
			initializeSociety();
		}
		societyManager.startAutomatons();
	}
	
	/**
	 * In onPause() the society state is saved.
	 */
	@Override
	protected void onPause() {
		super.onPause();
		societyManager.saveState();
	}

	/**
	 * Method called when the society must be recreated.<br/>
	 * It is called at the first start of application or when the society state fails to load.
	 */
	protected abstract void initializeSociety();

	
}
